var arrayChunk, queryCookie, queryLocalStorage, getLines, bubbleTop, getDefaultSetting, drawParagraph, splitPages, getPixelRatio, decryptTrd, isMobileBrowser, isWechatBrowser, drawAll, drawLast, isMobile, isWechat, renderTrd, objectPack = function(a) {
		var c, b = [];
		for (c in a) a.hasOwnProperty(c) && b.push(encodeURIComponent(c) + "=" + encodeURIComponent(a[c]));
		return b.join("&")
	},
	objectUnpack = function(a) {
		var d, e, f, b = {},
			c = ("?" === a[0] ? a.substr(1) : a).split("&");
		for (d = 0; d < c.length; d++) e = c[d].split("="), f = decodeURIComponent(e[0]), "" != f && (b[f] = decodeURIComponent(e[1] || ""));
		return b
	};
"undefined" != typeof CanvasRenderingContext2D && (CanvasRenderingContext2D.prototype.roundRect = function(a, b, c, d, e, f) {
	var g;
	return null == f && (f = [1, 1, 1, 1]), g = function(a) {
		return f[a] ? "arcTo" : "lineTo"
	}, 2 * e > c && (e = c / 2), 2 * e > d && (e = d / 2), this.beginPath(), this.moveTo(a + e, b), this[g(1)](a + c, b, a + c, b + d, e), this[g(2)](a + c, b + d, a, b + d, e), this[g(3)](a, b + d, a, b, e), this[g(0)](a, b, a + c, b, e), this.closePath(), this
}), arrayChunk = function(a, b) {
	if (!a.length) return [];
	void 0 === typeof b && (b = 10);
	var c, d, e, f = [];
	for (c = 0, d = a.length; d > c; c += b) e = a.slice(c, c + b), f.push(e);
	return f
}, queryCookie = function(a) {
	var b = document.cookie.match(new RegExp("(^| )" + a + "=([^;]*)(;|$)"));
	return null != b ? unescape(b[2]) : null
}, queryLocalStorage = function(a) {
	return window.localStorage.getItem(a) || null
}, getLines = function(a, b, c, d, e) {
	var f, g, h, i, j, k, l, m;
	for (m = c.split(""), j = [], f = m[0], b.font = e, g = h = 0, i = m.length; i > h; g = ++h) l = m[g], wordp = g > 1 ? m[g - 1] : "", g > 0 && (k = b.measureText(f + "" + l).width, d > k && "\n" != wordp ? f += "" + l : (j.push(f), f = l));
	return j.push(f), j
}, bubbleTop = function(a, b, c) {
	return a * (b + c)
}, getDefaultSetting = function() {
	queryCookie("cookie_font"), queryCookie("size")
}, drawParagraph = function(a, b, c, d, e, f, g, h, i) {
	var l, n, o, p, q, r;
	for (q = [], l = n = 0, o = a.length; o > n; l = ++n) p = a[l], r = c.measureText(p).width, c.fillStyle = f, c.font = d, q.push(c.fillText(p, h, bubbleTop(l + g / e / 2, g, i) + 2 * i));
	return q
}, splitPages = function(a, b, c, d) {
	var g, e = 4e3 / d;
	return (isWechat || isMobile) && (e = 1e3 / d), g = Math.floor(e / (parseInt(b) + c)), g = g > 1 ? g : 1, arrayChunk(a, g)
}, getPixelRatio = function(a) {
	var b = a.backingStorePixelRatio || a.webkitBackingStorePixelRatio || a.mozBackingStorePixelRatio || a.msBackingStorePixelRatio || a.oBackingStorePixelRatio || a.backingStorePixelRatio || 1;
	return (window.devicePixelRatio || 1) / b
}, decryptTrd = function(a, b) {
	var j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, c = "trd-" + a,
		d = "text-" + a,
		e = $("#" + c),
		f = $("#" + d),
		g = e.parent(".page_content,.page-content,.zhangjie"),
		h = e.find(".data-trda").val(),
		i = document.getElementById(c);
	if (b.s && (k = b.s.split(".", 2), l = trd.lib.CipherParams.create({
		ciphertext: trd.enc.Base64.parse(h)
	}), m = trd.ALGON.decrypt(l, trd.enc.Hex.parse(k[0] || ""), {
		iv: trd.enc.Hex.parse(k[1] || "")
	}), j = m.toString(trd.enc.Utf8)), j = b.msg ? b.msg : j, n = document.createElement("canvas"), o = n && n.getContext && "function" == typeof n.getContext("2d").fillText, isWechat || !o) return f.html(j.replace("。", "..")), !1;
	for (p = getPixelRatio(n.getContext("2d")), 
    q = n.getContext("2d"), r = e.width(), s = 16, t = 1.5 * s, u = g.css("color"), v = g.css("font-family"), g.hasClass("page-content") ? (w = queryLocalStorage("bgColor"), u = "night" == w ? "#999" : "#666", s = queryLocalStorage("fontSize") || 16, t = 1.5 * s) : g.hasClass("page_content") ? (w = queryLocalStorage("bgColor"), u = "night" == w ? "#999" : "#333", s = parseInt(queryLocalStorage("textFontSize")) || 18, t = parseInt(queryLocalStorage("textLineHeight")) || 1.7 * s) : (v = queryCookie("cookie_font") || "wryh", v = "st" == v ? 'SimSun,"宋体",STSong,STZhongsong,NSimSun' : '"Microsoft YaHei","微软雅黑",SimHei,STHeiti', s = parseInt(queryCookie("size")) || 16, t = 1.5 * s), x = "normal normal normal " + s + "px " + v, y = 0, z = 0, j = j.replace(/<p\s*>/gim, ""), j = j.replace(/<\/\s*p>/gim, "<br>"), A = "\n", (g.hasClass("page-content") || g.hasClass("zhangjie")) && (A = "\n\n"), j = j.replace(/<br\s*\/?>/gim, A), B = getLines(E, q, j, i.clientWidth - 2 * y, x), C = splitPages(B, t, z, p), f.html(""), D = 0; D < C.length; D++) E = document.createElement("canvas"), E.width = r, E.height = bubbleTop(C[D].length, t, z), F = E.getContext("2d"), drawParagraph(C[D], E, F, x, s, u, t, y, z), document.getElementById(d).appendChild(E)
}, isMobileBrowser = function() {
	return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
}, isWechatBrowser = function() {
	return /MicroMessenger/i.test(navigator.userAgent)
}, drawAll = function() {
	$(".textwrap").each(function(a, b) {
		renderTrd($(b).find(".data-trdpp").val())
	})
}, drawLast = function() {
	$(".textwrap").length && renderTrd($(".textwrap").last().find(".data-trdpp").val())
}, isMobile = isMobileBrowser(), isWechat = isWechatBrowser(), renderTrd = function(a) {
	var b = $("#trd-" + a).find(".data-trdkk").val() || "";
	b ? (b = objectUnpack(b), decryptTrd(a, b)) : $.get("/book-textreader", {
		p: a
	}, function(b) {
		$("#trd-" + a).find(".data-trdkk").val(objectPack(b)), decryptTrd(a, b)
	})
}, $(function() {
	drawAll(), $("#setup_font_yahei").click(drawAll), $("#setup_font_simsun").click(drawAll), $("#setup_size_minus").click(drawAll), $("#setup_size_max").click(drawAll), $(".toMode").click(drawAll), $(".read_night").click(drawAll), $(".incb").click(drawAll), $(".incs").click(drawAll), $("#fontup").click(drawAll), $("#fontdown").click(drawAll), isMobile || $(window).resizeend(drawAll)
});